home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / ^Php / php album / VERSION2 / thumbnail.php < prev   
Encoding:
PHP Script  |  2001-02-18  |  2.9 KB  |  89 lines

  1. <?php
  2.     /* thumbnail.php
  3.      * .net magazine (www.netmag.co.uk), issue 83
  4.      * Matt Kynaston, 2001
  5.      * Distributed under the GNU Public License - www.gnu.org/copyleft/gpl.html
  6.      *
  7.      * usage: thumbnail.php?file=filename.jpg
  8.      * This file outputs a thumnail image and from the file passed to it
  9.      * in the 'file' query string. 
  10.      * It uses the GD extension to manipulate the images - the current versions 
  11.      * can't handle GIF images for licensing reasons: check which version your
  12.      * host has installed before using it.
  13.      * If there is an error, or the file type is wrong, it generates a 
  14.      * 'no thumbnail available' image instead
  15.      *
  16.      * For more information on the image manipulation functions this script
  17.      * uses - and many more besides - see 'Image Functions' in the PHP Manual's 
  18.      * Function Reference.
  19.     */
  20.     
  21.     /* The next line tells the browser the image type being returned. Note
  22.      * that php scripts can return nearly any kind of file (images, wap pages,
  23.      * PDF documents, etc.) - so long as the correct content type is specified
  24.     */ 
  25.     Header('Content-type: image/png');
  26.     
  27.     /* GetImageSize returns an array:
  28.      *    $info[0] - horizontal size of image in pixels
  29.      *    $info[1] - vertical size of image in pixels
  30.      *    $info[2] - type of image (1=GIF, 2=JPEG, 3=PNG)
  31.     */
  32.     $info = GetImageSize("$file");
  33.     
  34.     
  35.     // is image big enough to need resizing?
  36.     if ($info[0] > MAX_XY || $info[1] > MAX_XY) {
  37.         // is image landscape?
  38.         if ($info[0] >= $info[1]) {
  39.             $width = MAX_XY;
  40.             $height = $info[1]*MAX_XY/$info[0];
  41.         // or portrait?
  42.         } else {
  43.             $height = MAX_XY;
  44.             $width = $info[0]*MAX_XY/$info[1];
  45.         }
  46.     } else {
  47.         // use original dimensions
  48.         $width = $info[0];
  49.         $height = $info[1];
  50.     }
  51.  
  52.     // create new image to hold thumbnail
  53.     $im = ImageCreate($width, $height);
  54.     
  55.     /* load original image, depending on type - see GetImageSize note
  56.      * above for description of file types
  57.     */
  58.     switch ($info[2]) {
  59.         case 2 : $im_big = ImageCreateFromJPEG("$file");
  60.             break;
  61.         case 3 : $im_big = ImageCreateFromPNG("$file");
  62.             break;
  63.         default: $im_big = "";
  64.     }
  65.     
  66.     // if image couldn't be loaded, create a 'no thumbnail avail' image instead
  67.     if (!$im_big) {
  68.         $width = 100;
  69.         $height = 100;
  70.         $im = ImageCreate($width, $height); // create blank image
  71.         $bgc = ImageColorAllocate($im, 0, 0, 0); // background color black
  72.         $tc  = ImageColorAllocate($im, 255, 255, 255); // text color white
  73.         ImageFilledRectangle($im, 0, 0, $width, $height, $bgc); // fill in image
  74.         ImageString($im, 3, 9, 36, "No thumbnail", $tc); // write text
  75.         ImageString($im, 3, 17, 48, "available", $tc);
  76.     
  77.     // otherwise, resize original image into thumbnail  
  78.     } else {
  79.         ImageCopyResized($im,$im_big,0,0,0,0,$width,$height,$info[0],$info[1]);
  80.     }
  81.     
  82.     // output the PNG image to the browser
  83.     ImagePNG($im);
  84.     
  85.     // clean up 
  86.     ImageDestroy($im_big);
  87.     ImageDestroy($im);
  88.     
  89. ?>